import { aesEncrypt } from "~/utils/encrypt";
import { GetBuyTemplates, GetBuyWorks } from "~/api";

const onJumpUrl = (url) => {
    const pageStore = usePageStore();
    const { origin, search } = GetUrlParams(url);
    const newUrl = `${origin}?${aesEncrypt(search)}`;

    if (pageStore.isMobile) {
        window.location.href = newUrl;
    } else {
        window.open(newUrl);
    }
};

export const useContent = () => {
    const keyword = ref("");
    const list = ref([]);
    const pagination = reactive({
        page: 1,
        size: 10,
    });
    const userStore = useUserStore();
    const pageStore = usePageStore();
    const isLoading = ref(false);
    const tabActive = ref(1);
    const tabList = reactive([
        {
            label: "已购模板",
            value: 1,
        },
        {
            label: "已购课件",
            value: 2,
        },
    ]);

    const filterList = computed(() => {
        return list.value.filter((item) => {
            if (item.info) {
                let name = item.info.name;
                return name ? name.includes(keyword.value) : false;
            } else {
                return false;
            }
        });
    });

    const renderList = computed(() => {
        return filterList.value.slice(
            (pagination.page - 1) * pagination.size,
            pagination.page * pagination.size
        );
    });

    const GetList = async () => {
        const ApiFunc = {
            1: GetBuyTemplates,
            2: GetBuyWorks,
        }[tabActive.value];

        isLoading.value = true;
        const res = await ApiFunc();
        isLoading.value = false;

        if (res.code == 200) {
            let data = res.data;
            list.value = data;
        } else {
            Message().error(res.msg);
        }
    };

    const onSearch = (value) => {
        pagination.page = 1;
        keyword.value = value;
    };

    const onEdit = (item) => {
        if (tabActive.value == 1) {
            if (item.info) {
                if (pageStore.isMobile) {
                    Message().warning("请在电脑端网站打开！网址www.ykjhr.com");
                } else {
                    let url = GetTemplateLink2(item.info.url, item.info.code);
                    onJumpUrl(url + "&uid=" + userStore.userInfo.id);
                }
            } else {
                Message().warning("该模板已失效，请重新购买");
            }
        } else {
            if (item.info) {
                navigateTo(
                    item.info.url.replace(
                        "mshare.vrnewg.com",
                        "kj-mshare.ykjhr.com"
                    ),
                    {
                        open: true,
                    }
                );
            } else {
                Message().warning("该课件作品已删除，有问题请联系客服");
            }
        }
    };

    watch(tabActive, () => {
        list.value = [];
        pagination.page = 1;
        GetList();
    });

    return {
        tabActive,
        tabList,
        filterList,
        list,
        pagination,
        renderList,
        isLoading,
        GetList,
        onSearch,
        onEdit,
    };
};
